package com.mxx.tomcat;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mxx.common.model.Response;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author itzgyw
 * @Date 2020-02-28
 * @Time 23:20
 * @Description:
 */
public class ResponseWriter {

    private ObjectMapper objectMapper;

    public ResponseWriter(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    public void writer(HttpServletResponse response, Object data) throws IOException {
        response.setStatus(200);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        objectMapper.writeValue(response.getWriter(), data);
    }

    public Object getFailureEntity(String failureMessage) {
        return new Response<>(500, failureMessage);
    }

    public ObjectMapper getObjectMapper() {
        return objectMapper;
    }
}
